home *** CD-ROM | disk | FTP | other *** search
- Path: news.itmel.bhp.com.au!usenet
- From: Chris Kuan <kuan.chris.ch@bhp.com.au>
- Newsgroups: comp.lang.c++,comp.sys.hp.hpux
- Subject: Problem combining signal() with exceptions on HP-UX (ugh!)
- Date: 20 Mar 1996 08:58:13 GMT
- Organization: BHP Information Technology
- Message-ID: <4iohb5$e9i@gossamer.itmel.bhp.com.au>
- NNTP-Posting-Host: 134.18.242.60
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.2N (Windows; I; 16bit)
-
- Sorry to post a whole slab of code, but it is self-contained...
-
- I'm running HP CC on HP-UX 9, and trying to catch signals and turn them
- into exceptions.
-
- The example program below behaves inconsistently - sometimes when I press
- Control-C (SIGINT) within cktest(), it gets caught OK. Other times it gets caught
- by the catch block in main(). Other times an Abort(coredump) happens just after
- the cout within ckDefaultSignalHandler.
-
- Question : how can I get signalling and exception handling to work consistently?
- - and don't say, "Don't do it." It's beyond my control as to WHY this is
- necessary :-(
-
- ==================================================================
- #include <unistd.h>
- #include <signal.h>
-
- #include <iostream.h>
-
- class SignalException
- {
- public:
-
- SignalException(int signalNumber) : mSignalNumber( signalNumber){};
-
- int SignalNumber( void){ return mSignalNumber;};
-
- private :
-
- int mSignalNumber;
-
- };
-
-
- double cktest( int ckflag);
-
- static void ckDefaultSignalHandler(int signalNumber);
-
- void main(void)
- {
- int caught = 0;
- try
- {
- signal(SIGINT, &ckDefaultSignalHandler);
-
- while ( caught == 0)
- {
-
- int testflag = 1;
- double result = cktest( testflag);
- cout << "test() = " << result << endl;
-
-
- cout << " main() for loop..." << endl;
- for (;;)
- ;
-
- } // while
-
- } // try
-
- catch( SignalException exception)
- {
- if ( exception.SignalNumber() == SIGFPE)
- {
- cout << "\nA floating point exception occurred in main()!"
- << endl;
- caught = 1;
- }
- else
- {
- cout << "\nAn unexpected signal occurred in main()!"
- << "\nSignal number = "
- << exception.SignalNumber()
- << endl;
- }
- } // catch
-
- catch(...)
- {
- cout << "An unexpected exception was caught in main!" << endl;
- }
-
- cout << "Leaving main()... caught = " << caught << endl;
-
- } // main
-
-
- double cktest( int ckflag)
- {
-
- cout << "entering test()..." << endl;
-
-
- while ( ( ckflag < 3 ) )
- {
- cout << "ckflag = " << ++ckflag << endl;
- try
- {
- double ckdouble1 = 1.0;
- double ckdouble2 = 100.0;
- double ckdouble3 = ckdouble2 / ckdouble1;
-
- cout << " test() for loop..." << endl;
- for (;;)
- ;
-
- return( ckdouble3);
- } // try
-
- catch( SignalException exception)
- {
- if ( exception.SignalNumber() == SIGFPE)
- {
- cout << "\nA floating point exception occurred in test()!"
- << endl;
- }
- else
- {
- cout << "\nAn unexpected signal occurred in test()!"
- << "\nSignal number = "
- << exception.SignalNumber()
- << endl;
- }
-
- } // catch
-
- catch(...)
- {
- cout << "An unexpected exception was caught in test!" << endl;
- }
-
-
- } // while
-
- cout << "leaving test()..." << endl;
-
-
- } // test
-
- static void ckDefaultSignalHandler(int signalNumber)
- {
- signal(signalNumber, &ckDefaultSignalHandler);
- cout << "\nckDefaultSignalHandler called. signalNumber = " << signalNumber <<
- endl;
-
- throw SignalException(signalNumber);
-
- }
-
-
- ================================================================================
- Chris Kuan : Email kuan.chris.ch@bhp.com.au
- Systems Integration Services : Voice +61 42 75 5657
- BHP Information Technology, Wollongong Region : Fax +61 42 75 5500
- ================================================================================
-
-
-